/**
 * framebufferhander.h
 * 
 * @author Zsolt Horváth 
 * @date   3. 4. 2011
 * 
 */

#ifndef FRAMEBUFFER_HANDLER_H_
#define FRAMEBUFFER_HANDLER_H_

#include <vector>

#include <GL/glew.h>
#include <GL/glu.h>

#include "framebuffer.h"

/**
 * Framebuffer handler
 * 
 * First added buffer must contain texture depth attachment
 * Class automatically handles cleaning garbage, so it deletes
 * added buffers when deleting instance
 */
class FramebufferHandler {
    
    public:
        /**
         * Constructor
         */
        FramebufferHandler ();
        
        /**
         * Desctructor
         */
        ~FramebufferHandler ();
        
        /**
         * Add framebuffer
         * 
         * @param Framebuffer *framebuffer - Framebuffer instace
         * @param bool def - true if adding default buffer
         */
        void addBuffer (Framebuffer *framebuffer, bool def = false);
        
        /**
         * Bind first buffer
         * 
         * @param bool def - true if binding default buffer
         */
        void bindFirst (bool def = false);
        
        /**
         * Bind next buffer
         * 
         * @param bool def - true if binding default buffer
         */
        void bindNext (bool def = false);
        
        /**
         * Bind current buffer
         * 
         * @param bool def - true if binding default buffer
         */        
        void bind (bool def = false);
        
        /**
         * Resize framebuffers' textures
         */
        void resizeTextures (int width, int height);

    protected:
        /**
         * Buffers without default buffer
         */
        vector <Framebuffer *> buffers;
        
        /**
         * Default framebuffer
         * Should contain final rendered image
         */
        Framebuffer * defaultFramebuffer;
        
        /**
         * Current framebuffer index in vector
         */
        unsigned int current;
        
        /**
         * Automatically binds textures of previous
         * framebuffer, which contains the rendered image
         * 
         * @param bool def - use default framebuffer?
         */
        void bindTextures(bool def);
    
};

#endif /* FRAMEBUFFER_HANDLER_H_ */